package com.fowo.api.datainterface.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.fowo.api.common.model.R;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Tag(name = "枚举接口")
@AllArgsConstructor
@Slf4j
@RestController
@RequestMapping("/api/sys/enums")
public class EnumController {


    @GetMapping("/list")
    public R<Map<String, Object>> list() {
        Map<String, Object> map = new HashMap<>();
        map.put("事务类型", "DatainterfaceTypeKind");
        map.put("接口类型", "DataTypeKind");
        map.put("映射列类型", "MappingColTypeKind");
        map.put("映射列固定类型", "MappingColFixedKind");
        map.put("接口请求方式类型", "RequestMethodKind");
        map.put("映射数据类型", "MappingFieldTypeKind");
        map.put("参数数据类型", "RequestFieldTypeKind");
        return R.data(map);
    }

    @GetMapping("/getByName")
    public R getByName(@RequestParam(value = "name", required = true) String className) {
        try {
            Class<? extends Enum> clazz = ClassUtil.loadClass(StrUtil.concat(true, "com.fowo.api.datainterface.constants.", className));
            if (ObjectUtil.isEmpty(clazz)) {
                return null;
            }
            final Enum<?>[] enums = clazz.getEnumConstants();
            if (ObjectUtil.isEmpty(enums)) {
                return null;
            }
            final List<Map> list = new ArrayList<>(enums.length);
            for (Enum<?> e : enums) {
                Map<Object, Object> build = MapUtil.builder().put("id", ReflectUtil.getFieldValue(e, "msg")).put("name", ReflectUtil.getFieldValue(e, "value")).build();
                list.add(build);
            }
            return R.data(list);
        } catch (Exception ex) {
        }
        return R.fail("发生异常！");
    }


    @GetMapping("/getRequestMethodKindByName")
    public R getRequestMethodKindByName(@RequestParam(value = "name", required = true) String className,
                                        @RequestParam(value = "dataType", required = true) String dataType
    ) {
        try {
            Class<? extends Enum> clazz = ClassUtil.loadClass(StrUtil.concat(true, "com.fowo.api.datainterface.constants.", className));
            if (ObjectUtil.isEmpty(clazz)) {
                return null;
            }
            final Enum<?>[] enums = clazz.getEnumConstants();
            if (ObjectUtil.isEmpty(enums)) {
                return null;
            }
            final List<Map> list = new ArrayList<>(enums.length);
            for (Enum<?> e : enums) {
                String dt = StrUtil.toString(ReflectUtil.getFieldValue(e, "dataType"));
                if (StrUtil.contains(dt, dataType.toUpperCase())) {
                    Map<Object, Object> build = MapUtil.builder().put("id", ReflectUtil.getFieldValue(e, "msg")).put("name", ReflectUtil.getFieldValue(e, "value")).build();
                    list.add(build);
                }
            }
            return R.data(list);
        } catch (Exception ex) {
        }
        return R.fail("发生异常！");
    }
}
